package com.sxx.mallcart.controller;

import com.sxx.commons.constant.AuthConstant;
import com.sxx.commons.vo.MemberVO;
import com.sxx.mallcart.interceptor.AuthIntercepter;
import com.sxx.mallcart.service.ICartService;
import com.sxx.mallcart.vo.Cart;
import com.sxx.mallcart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Auther: sunxx
 * @Date: 2024/8/29 - 08 - 29 - 0:39
 * @Description: com.sxx.mallcart.controller
 * @version: 1.0
 */
@Controller
public class CartController {
    @Autowired
    private ICartService cartService;

    /*
     * 查询所有当前用户的购物车内选中的提交确认的商品信息
     * */
    @GetMapping("/getCartToConfirm")
    @ResponseBody
    public List<CartItem> getCartToConfirm(){
        return cartService.getCartToConfirm();
    }

    @GetMapping("/cart_list")
    public String queryCartList(Model model){
        Cart cart = cartService.getCartList();
        if (cart == null){
            return "http://jiaxuanmall.aisa:8070/index";
        }
        model.addAttribute("cart",cart);
        return "/cartList";
    }

/*
* 加入购物车
* */
    @GetMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("proNum")Integer proNum,
                          Model model){
        //把商品添加到购物车中
        CartItem item = null;
        try {
            item = cartService.addCart(skuId,proNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("item",item);
        return "success";
    }
}
